package com.ruoyi.common.utils.msg.util.format;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ReservoirCapacityCalculator {

    // 完整的水位-库容数据映射表（精确到厘米）
    private static final Map<String, Double> CAPACITY_MAP = new HashMap<>();

    // 原来的整米水位数据（保持兼容性）
    private static final double[] WATER_LEVELS = {
            77, 77.50, 78, 78.50, 79, 79.50, 80, 80.50, 81, 81.50, 82, 82.50, 83, 83.50, 84, 84.50, 85, 85.50, 86, 86.50, 87, 87.50, 88, 88.50, 89, 89.50, 90, 90.50, 91, 91.50, 92, 93
    };

    private static final double[] CAPACITIES = {
            39.3, 47.31, 56.26, 66.37, 77.86, 90.92, 105.73, 122.65, 142.04, 163.50, 186.63, 211.94, 239.94, 271.35, 306.88, 346.01, 388.23, 433.49,
            481.78, 534.02, 591.15, 652.51, 717.44, 786.69, 861.41, 943.73, 1026.05, 1115.83, 1205.61, 1300.11, 1394.6, 1595.47
    };

    static {
        // 初始化完整的水位库容映射表
        initializeCapacityMap();
    }

    /**
     * 初始化完整的水位库容映射表（精确到厘米）
     */
    private static void initializeCapacityMap() {
        // 77米水位数据
        addLevelData(77, new double[][]{
                {39.30, 40.83, 42.39, 43.99, 45.63, 47.31, 49.02, 50.78, 52.56, 54.39},
                {39.45, 40.98, 42.55, 44.15, 45.79, 47.48, 49.19, 50.95, 52.74, 54.57},
                {39.60, 41.14, 42.71, 44.31, 45.96, 47.65, 49.37, 51.13, 52.92, 54.76},
                {39.76, 41.29, 42.87, 44.48, 46.13, 47.83, 49.54, 51.31, 53.10, 54.95},
                {39.91, 41.45, 43.03, 44.64, 46.30, 48.00, 49.72, 51.49, 53.29, 55.13},
                {40.06, 41.61, 43.19, 44.81, 46.47, 48.17, 49.90, 51.67, 53.47, 55.32},
                {40.21, 41.76, 43.35, 44.97, 46.63, 48.34, 50.07, 51.84, 53.65, 55.51},
                {40.36, 41.82, 43.51, 45.13, 46.80, 48.51, 50.25, 52.02, 53.84, 55.69},
                {40.52, 42.07, 43.67, 45.30, 46.97, 48.68, 50.42, 52.20, 54.02, 55.88},
                {40.67, 42.23, 43.83, 45.46, 47.14, 48.85, 50.60, 52.38, 54.20, 56.07}
        });

        // 78米水位数据
        addLevelData(78, new double[][]{
                {56.26, 58.17, 60.14, 62.16, 64.23, 66.37, 68.55, 70.79, 73.09, 75.44},
                {56.45, 58.36, 60.34, 62.36, 64.44, 66.58, 68.77, 71.02, 73.32, 75.68},
                {56.64, 58.56, 60.54, 62.57, 64.65, 66.80, 68.99, 71.25, 73.56, 75.92},
                {56.83, 58.76, 60.74, 62.78, 64.87, 67.02, 69.22, 71.48, 73.69, 76.16},
                {57.02, 58.95, 60.94, 62.98, 65.08, 67.24, 69.44, 71.71, 74.03, 76.40},
                {57.21, 59.15, 61.15, 63.19, 65.30, 67.46, 69.67, 71.94, 74.26, 76.65},
                {57.40, 59.35, 61.35, 63.39, 65.51, 67.67, 69.89, 72.17, 74.50, 76.89},
                {57.59, 59.54, 61.55, 63.60, 65.72, 67.89, 70.11, 72.40, 74.73, 77.13},
                {57.78, 59.74, 61.75, 63.81, 65.94, 68.11, 70.34, 72.63, 74.97, 77.37},
                {57.97, 59.94, 61.95, 64.01, 66.15, 68.33, 70.56, 72.86, 75.20, 77.61}
        });

        // 79米水位数据
        addLevelData(79, new double[][]{
                {77.86, 80.33, 82.87, 85.48, 88.16, 90.92, 93.74, 96.63, 99.59, 102.63},
                {78.10, 80.58, 83.13, 85.74, 88.43, 91.20, 94.02, 96.92, 99.89, 102.94},
                {78.35, 80.83, 83.39, 86.01, 88.71, 91.48, 94.31, 97.22, 100.09, 103.25},
                {78.60, 81.09, 83.65, 86.28, 88.98, 91.76, 94.60, 97.51, 100.40, 103.56},
                {78.84, 81.34, 83.91, 86.55, 89.26, 92.04, 94.89, 97.81, 100.70, 103.87},
                {79.09, 81.60, 84.17, 86.82, 89.54, 92.33, 95.18, 98.11, 101.00, 104.18},
                {79.34, 81.85, 84.43, 87.08, 89.81, 92.61, 95.47, 98.40, 101.31, 104.49},
                {79.58, 82.10, 84.69, 87.35, 90.09, 92.89, 95.76, 98.70, 101.61, 104.80},
                {79.83, 82.36, 84.95, 87.62, 90.36, 93.17, 96.05, 98.99, 101.92, 105.11},
                {80.08, 82.61, 85.21, 87.89, 90.64, 93.45, 96.34, 99.29, 102.22, 105.42}
        });

        // 80米水位数据
        addLevelData(80, new double[][]{
                {105.73, 108.91, 112.20, 115.58, 119.07, 122.65, 126.33, 130.11, 133.99, 137.91},
                {105.95, 109.23, 112.53, 115.92, 119.42, 123.01, 126.70, 130.49, 134.38, 138.37},
                {106.28, 109.56, 112.87, 116.27, 119.78, 123.38, 127.08, 130.88, 134.78, 138.78},
                {106.61, 109.89, 113.21, 116.62, 120.14, 123.75, 127.46, 131.27, 135.18, 139.19},
                {106.94, 110.22, 113.55, 116.97, 120.50, 124.12, 127.84, 131.66, 135.58, 139.59},
                {107.27, 110.55, 113.89, 117.32, 120.86, 124.49, 128.22, 132.05, 135.98, 140.00},
                {107.59, 110.88, 114.22, 117.67, 121.21, 124.85, 128.59, 132.43, 136.37, 140.41},
                {107.92, 111.21, 114.56, 118.02, 121.57, 125.22, 128.97, 132.82, 136.77, 140.81},
                {108.25, 111.54, 114.90, 118.37, 121.93, 125.59, 129.35, 133.21, 137.17, 141.22},
                {108.58, 111.87, 115.24, 118.72, 122.29, 125.96, 129.73, 133.60, 137.57, 141.63}
        });

        // 81米水位数据
        addLevelData(81, new double[][]{
                {142.04, 146.20, 150.42, 154.72, 159.07, 163.50, 167.99, 172.55, 177.18, 181.87},
                {142.45, 146.62, 150.85, 155.15, 159.51, 163.94, 168.44, 173.01, 177.64, 182.34},
                {142.87, 147.04, 151.28, 155.59, 159.95, 164.39, 168.90, 173.47, 178.11, 182.82},
                {143.28, 147.46, 151.71, 156.02, 160.39, 164.84, 169.35, 173.93, 178.58, 183.29},
                {143.70, 147.88, 152.14, 156.46, 160.84, 165.29, 169.81, 174.40, 179.05, 183.77},
                {144.12, 148.31, 152.57, 156.89, 161.28, 165.74, 170.27, 174.86, 179.52, 184.25},
                {144.53, 148.73, 153.00, 157.33, 161.72, 166.19, 170.72, 175.32, 179.99, 184.72},
                {144.95, 149.15, 153.43, 157.76, 162.17, 166.64, 171.18, 175.79, 180.46, 185.20},
                {145.36, 149.57, 153.86, 158.20, 162.61, 167.09, 171.63, 176.25, 180.93, 185.67},
                {145.78, 149.99, 154.29, 158.63, 163.05, 167.54, 172.09, 176.71, 181.40, 186.15}
        });

        // 82米水位数据
        addLevelData(82, new double[][]{
                {186.63, 191.48, 196.43, 201.50, 206.67, 211.94, 217.33, 222.82, 228.42, 234.13},
                {187.11, 191.97, 196.93, 202.01, 207.19, 212.37, 217.87, 223.38, 228.99, 234.71},
                {187.60, 192.47, 197.44, 202.53, 207.72, 213.01, 218.42, 223.94, 229.56, 235.29},
                {188.08, 192.96, 197.95, 203.05, 208.25, 213.55, 218.97, 224.50, 230.12, 235.87},
                {188.57, 193.46, 198.45, 203.56, 208.77, 214.09, 219.52, 225.06, 230.69, 236.45},
                {189.05, 193.95, 198.96, 204.08, 209.30, 214.63, 220.07, 225.62, 231.26, 237.03},
                {189.54, 194.45, 199.47, 204.60, 209.83, 215.17, 220.62, 226.18, 231.83, 237.61},
                {190.02, 194.94, 199.97, 205.11, 210.35, 215.71, 221.17, 226.74, 232.40, 238.19},
                {190.51, 195.44, 200.48, 205.63, 210.88, 216.25, 221.72, 227.30, 232.97, 238.77},
                {190.99, 195.93, 200.99, 206.15, 211.41, 216.79, 222.27, 227.86, 233.54, 239.35}
        });

        // 83米水位数据
        addLevelData(83, new double[][]{
                {239.94, 245.89, 252.21, 258.29, 264.74, 271.35, 278.12, 285.06, 292.17, 299.44},
                {240.53, 246.50, 252.63, 258.93, 265.40, 272.02, 278.81, 285.77, 292.89, 300.18},
                {241.13, 247.11, 253.26, 259.58, 266.06, 272.70, 279.50, 286.48, 293.62, 300.92},
                {241.72, 247.72, 253.89, 260.22, 266.72, 273.38, 280.20, 287.19, 294.35, 301.68},
                {242.32, 248.33, 254.52, 260.87, 267.38, 274.05, 280.89, 287.90, 295.07, 302.42},
                {242.91, 248.95, 255.15, 261.51, 268.04, 274.73, 281.59, 288.61, 295.80, 303.17},
                {243.51, 249.56, 255.77, 262.16, 268.70, 275.41, 282.28, 289.32, 296.53, 303.91},
                {244.10, 250.17, 256.40, 262.80, 269.36, 276.08, 282.97, 290.03, 297.25, 304.65},
                {244.70, 250.78, 257.03, 263.45, 270.02, 276.76, 283.67, 290.74, 297.98, 305.40},
                {245.29, 251.39, 257.66, 264.09, 270.68, 277.44, 284.36, 291.45, 298.71, 306.14}
        });

        // 84米水位数据
        addLevelData(84, new double[][]{
                {306.88, 314.46, 322.16, 329.99, 337.94, 346.01, 354.21, 362.53, 370.97, 379.54},
                {307.63, 315.23, 322.94, 330.78, 338.74, 346.81, 355.04, 363.37, 371.82, 380.40},
                {308.39, 316.00, 323.72, 331.58, 339.55, 347.62, 355.87, 364.21, 372.68, 381.27},
                {309.15, 316.77, 324.50, 332.37, 340.36, 348.43, 356.70, 365.06, 373.54, 382.14},
                {309.91, 317.54, 325.29, 333.17, 341.16, 349.23, 357.53, 365.90, 374.39, 383.01},
                {310.67, 318.31, 326.07, 333.96, 341.97, 350.04, 358.37, 366.75, 375.25, 383.88},
                {311.42, 319.08, 326.85, 334.76, 342.78, 350.85, 359.20, 367.59, 376.11, 384.75},
                {312.18, 319.85, 327.64, 335.55, 343.58, 351.65, 360.03, 368.43, 376.96, 385.62},
                {312.94, 320.62, 328.42, 336.35, 344.39, 352.46, 360.86, 369.28, 377.82, 386.49},
                {313.70, 321.39, 329.20, 337.14, 345.20, 353.27, 361.69, 370.12, 378.68, 387.63}
        });

        // 85米水位数据
        addLevelData(85, new double[][]{
                {388.23, 397.04, 405.97, 415.02, 424.20, 433.49, 442.90, 452.44, 462.10, 471.88},
                {389.11, 397.93, 406.87, 415.92, 425.12, 434.43, 443.85, 453.40, 463.07, 472.87},
                {389.99, 398.82, 407.78, 416.86, 426.05, 435.37, 444.80, 454.37, 464.05, 473.86},
                {390.87, 399.71, 408.68, 417.73, 426.98, 436.31, 445.76, 455.33, 465.03, 474.85},
                {391.75, 400.61, 409.59, 418.64, 427.91, 437.25, 446.71, 456.30, 466.01, 475.84},
                {392.63, 401.50, 410.49, 419.54, 428.84, 438.19, 447.67, 457.27, 466.99, 476.83},
                {393.51, 402.39, 411.40, 420.45, 429.70, 439.13, 448.62, 458.23, 467.96, 477.82},
                {394.39, 403.29, 412.30, 421.35, 430.63, 440.07, 449.57, 459.20, 468.94, 478.81},
                {395.27, 404.18, 413.21, 422.26, 431.56, 441.01, 450.53, 460.16, 469.92, 479.80},
                {396.15, 405.07, 414.11, 423.16, 432.49, 441.95, 451.48, 461.13, 470.90, 480.79}
        });

        // 86米水位数据
        addLevelData(86, new double[][]{
                {481.78, 491.83, 502.08, 512.54, 523.18, 534.02, 545.06, 556.29, 567.71, 579.34},
                {482.78, 492.85, 503.12, 513.60, 524.26, 535.12, 546.18, 557.43, 568.87, 580.52},
                {483.79, 493.88, 504.17, 514.66, 525.34, 536.22, 547.30, 558.57, 570.03, 581.70},
                {484.79, 494.90, 505.21, 515.73, 526.43, 537.33, 548.42, 559.71, 571.19, 582.88},
                {485.80, 495.93, 506.26, 516.79, 527.51, 538.43, 549.55, 560.85, 572.36, 584.06},
                {486.80, 496.95, 507.31, 517.86, 528.60, 539.54, 550.67, 562.00, 573.62, 585.24},
                {487.81, 497.98, 508.35, 518.92, 529.68, 540.64, 551.79, 563.14, 574.68, 586.43},
                {488.81, 499.00, 509.40, 519.98, 530.76, 541.74, 552.92, 564.28, 575.85, 587.60},
                {489.82, 500.03, 510.44, 521.05, 531.86, 542.85, 554.04, 565.42, 577.01, 588.78},
                {490.83, 501.05, 511.49, 522.11, 532.93, 543.95, 555.16, 566.56, 578.17, 589.96}
        });

        // 87米水位数据
        addLevelData(87, new double[][]{
                {591.15, 603.14, 615.27, 627.54, 639.95, 652.51, 665.21, 678.05, 691.04, 704.17},
                {592.35, 604.35, 616.49, 628.78, 641.20, 653.78, 666.49, 679.35, 692.35, 705.49},
                {593.55, 605.56, 617.72, 630.02, 642.46, 655.05, 667.77, 680.65, 693.66, 706.82},
                {594.75, 606.77, 618.95, 631.26, 643.71, 656.32, 669.05, 681.95, 694.97, 708.15},
                {595.95, 607.99, 620.17, 632.50, 644.97, 657.59, 670.33, 683.25, 696.29, 709.47},
                {597.15, 609.20, 621.40, 633.74, 646.23, 658.87, 671.62, 684.55, 697.60, 710.80},
                {598.36, 610.41, 622.63, 634.98, 647.48, 660.14, 672.90, 685.84, 698.91, 712.13},
                {599.56, 611.63, 623.85, 636.22, 648.74, 661.41, 674.18, 687.14, 700.23, 713.45},
                {600.76, 612.84, 625.08, 637.46, 649.99, 662.68, 675.46, 688.44, 701.54, 714.78},
                {601.96, 614.05, 626.31, 638.70, 651.25, 663.95, 676.74, 689.74, 702.85, 716.11}
        });

        // 88米水位数据
        addLevelData(88, new double[][]{
                {717.44, 730.88, 744.53, 758.38, 772.43, 786.69, 801.15, 815.82, 830.68, 845.76},
                {718.78, 732.24, 745.91, 759.78, 773.85, 788.13, 802.61, 817.30, 832.18, 847.25},
                {720.12, 733.61, 747.30, 761.19, 775.28, 789.58, 804.08, 818.29, 833.69, 848.81},
                {721.47, 734.97, 748.68, 762.59, 776.70, 791.02, 805.55, 820.27, 835.20, 850.34},
                {722.81, 736.34, 750.07, 764.00, 778.13, 792.47, 807.01, 821.76, 836.71, 851.86},
                {724.16, 737.70, 751.45, 765.40, 779.56, 793.92, 808.48, 823.25, 838.22, 853.39},
                {725.50, 739.07, 752.84, 766.81, 780.98, 795.36, 809.95, 824.73, 839.72, 854.92},
                {726.84, 740.43, 754.22, 768.21, 782.41, 796.81, 811.41, 826.22, 841.23, 856.44},
                {728.19, 741.80, 755.61, 769.62, 783.83, 798.25, 812.88, 827.70, 842.74, 857.97},
                {729.53, 743.16, 756.99, 771.02, 785.26, 799.70, 814.36, 829.19, 844.25, 859.50}
        });

        // 89米水位数据
        addLevelData(89, new double[][]{
                {861.41, 876.51, 892.22, 908.14, 924.27, 943.73, 957.20, 973.99, 991.00, 1008.25},
                {862.57, 878.05, 893.84, 909.18, 925.90, 944.28, 958.88, 975.69, 992.72, 1009.99},
                {864.12, 879.60, 895.39, 911.39, 927.53, 945.94, 960.58, 977.39, 994.45, 1011.73},
                {865.67, 881.15, 896.98, 913.00, 929.16, 946.60, 962.24, 979.09, 996.17, 1013.48},
                {867.22, 882.70, 898.57, 914.62, 930.79, 947.25, 963.92, 980.79, 997.90, 1015.22},
                {868.77, 884.25, 900.17, 916.23, 932.42, 948.91, 965.60, 982.48, 999.62, 1016.97},
                {870.31, 885.79, 901.76, 917.84, 934.05, 950.57, 967.28, 984.18, 1001.35, 1018.77},
                {871.86, 887.34, 903.35, 919.46, 936.68, 952.22, 968.96, 985.89, 1003.07, 1020.45},
                {873.41, 888.89, 904.94, 921.07, 938.31, 953.88, 970.64, 987.58, 1004.80, 1022.20},
                {874.96, 890.44, 906.53, 922.68, 940.94, 955.54, 972.32, 989.29, 1006.52, 1023.94}
        });

        // 90米水位数据
        addLevelData(90, new double[][]{
                {1026.05, 1043.28, 1060.95, 1078.71, 1096.54, 1115.83, 1132.45, 1150.53, 1168.65, 1186.89},
                {1027.45, 1045.04, 1062.72, 1080.49, 1098.33, 1116.25, 1134.25, 1152.34, 1170.47, 1188.72},
                {1029.21, 1046.81, 1064.50, 1082.27, 1100.12, 1118.05, 1136.06, 1154.15, 1172.29, 1190.55},
                {1030.97, 1048.58, 1066.27, 1084.05, 1101.91, 1119.86, 1137.87, 1155.96, 1174.12, 1192.38},
                {1032.73, 1050.34, 1068.05, 1085.84, 1116.21, 1121.66, 1139.68, 1157.77, 1175.94, 1194.21},
                {1034.49, 1052.11, 1069.82, 1087.62, 1105.49, 1123.46, 1141.49, 1159.59, 1177.77, 1196.05},
                {1036.25, 1053.88, 1071.60, 1089.40, 1107.29, 1125.26, 1143.29, 1161.40, 1179.59, 1197.88},
                {1038.01, 1055.46, 1073.37, 1100.47, 1109.08, 1127.05, 1145.10, 1163.21, 1181.41, 1199.71},
                {1039.77, 1057.41, 1075.17, 1092.97, 1111.87, 1128.86, 1146.91, 1165.02, 1183.24, 1201.54},
                {1041.53, 1059.18, 1076.93, 1094.75, 1113.66, 1130.66, 1148.72, 1166.83, 1185.06, 1203.37}
        });

        // 91米水位数据
        addLevelData(91, new double[][]{
                {1205.61, 1223.62, 1248.15, 1276.04, 1290.51, 1300.11, 1317.31, 1336.37, 1355.54, 1374.81},
                {1207.05, 1225.47, 1251.02, 1278.65, 1291.39, 1301.25, 1319.21, 1338.28, 1357.46, 1376.76},
                {1208.89, 1227.26, 1254.87, 1280.52, 1292.28, 1302.14, 1321.11, 1340.20, 1359.39, 1378.71},
                {1210.73, 1229.17, 1256.73, 1281.39, 1293.16, 1304.04, 1323.01, 1342.12, 1361.32, 1380.66},
                {1212.57, 1231.03, 1259.60, 1283.27, 1294.04, 1305.93, 1324.92, 1344.03, 1363.24, 1382.60},
                {1214.41, 1234.88, 1262.46, 1284.14, 1295.93, 1307.82, 1326.82, 1345.95, 1365.17, 1384.55},
                {1216.25, 1236.73, 1265.32, 1286.01, 1296.81, 1309.72, 1328.73, 1347.87, 1367.10, 1386.50},
                {1218.09, 1239.59, 1268.18, 1287.88, 1297.69, 1311.61, 1330.64, 1349.78, 1369.02, 1388.45},
                {1219.93, 1242.44, 1271.04, 1288.76, 1298.58, 1313.51, 1332.54, 1351.70, 1370.95, 1390.40},
                {1221.77, 1245.29, 1273.91, 1289.63, 1299.46, 1315.40, 1334.45, 1353.62, 1372.88, 1392.35}
        });

        // 92米水位数据（只有部分数据）
        addLevelData(92, new double[][]{
                {1394.60, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        });
    }

    /**
     * 添加水位数据到映射表
     * @param baseLevel 基础水位（整数米）
     * @param data 厘米数据表
     */
    private static void addLevelData(int baseLevel, double[][] data) {
        for (int decimeter = 0; decimeter < 10; decimeter++) {  // 分米：0-9
            for (int centimeter = 0; centimeter < 10; centimeter++) {  // 厘米：0-9
                // 修复：厘米作为行索引，分米作为列索引
                double capacity = data[centimeter][decimeter];
                if (capacity > 0) {  // 只添加有效数据
                    double level = baseLevel + decimeter * 0.1 + centimeter * 0.01;
                    CAPACITY_MAP.put(String.format("%.2f", level), capacity);
                }
            }
        }
    }

    /**
     * 精确查询（使用完整的水位库容关系表）
     * @param waterLevel 水位值（精确到厘米）
     * @return 库容（万立方米），如果找不到精确匹配则返回null
     */
    public static Double preciseQuery(double waterLevel) {
        String key = String.format("%.2f", waterLevel);
        return CAPACITY_MAP.get(key);
    }

    /**
     * 增强的线性插值法（支持精确到厘米的插值）
     * @param waterLevel 水位值（77-93米之间，支持小数）
     * @return 库容（万立方米）
     */
    public static double linearInterpolation(double waterLevel) {
        // 精确查询
        Double preciseResult = preciseQuery(waterLevel);
        if (preciseResult != null) {
            return preciseResult;
        }

        // 检查水位范围
        if (waterLevel < 77.0 || waterLevel > 93.0) {
            throw new IllegalArgumentException("水位超出有效范围(77-93米)");
        }

        // 使用完整数据表进行插值
        double[] levels = CAPACITY_MAP.keySet().stream()
                .mapToDouble(Double::parseDouble)
                .sorted()
                .toArray();

        int index = Arrays.binarySearch(levels, waterLevel);
        if (index >= 0) {
            return CAPACITY_MAP.get(String.format("%.2f", levels[index]));
        }

        // 线性插值计算
        int upperIndex = -index - 1;
        int lowerIndex = upperIndex - 1;

        if (lowerIndex < 0 || upperIndex >= levels.length) {
            // 如果超出精确数据范围，使用原来的整米数据插值
            return originalLinearInterpolation(waterLevel);
        }

        double x0 = levels[lowerIndex];
        double x1 = levels[upperIndex];
        double y0 = CAPACITY_MAP.get(String.format("%.2f", x0));
        double y1 = CAPACITY_MAP.get(String.format("%.2f", x1));

        double result = y0 + (y1 - y0) * (waterLevel - x0) / (x1 - x0);

        // 保留两位小数
        return Math.round(result * 100) / 100.0;
    }

    /**
     * 原来的整米数据线性插值（保持兼容性）
     */
    private static double originalLinearInterpolation(double waterLevel) {
        int index = Arrays.binarySearch(WATER_LEVELS, waterLevel);
        if (index >= 0) {
            return CAPACITIES[index];
        }

        int upperIndex = -index - 1;
        int lowerIndex = upperIndex - 1;

        double x0 = WATER_LEVELS[lowerIndex];
        double x1 = WATER_LEVELS[upperIndex];
        double y0 = CAPACITIES[lowerIndex];
        double y1 = CAPACITIES[upperIndex];

        double result = y0 + (y1 - y0) * (waterLevel - x0) / (x1 - x0);

        return Math.round(result * 100) / 100.0;
    }

    /**
     * 增强的线性插值法（支持Object类型输入）
     * @param object 水位值（77-93米之间）
     * @return 库容（万立方米）字符串
     */
    public static String linearInterpolation2(Object object) {
        Double waterLevel = toDoubleSafe(object);
        if (waterLevel == null) {
            throw new IllegalArgumentException("无效的水位值");
        }

        double result = linearInterpolation(waterLevel);
        return String.format("%.2f", result);
    }

    /**
     * 安全转换为Double类型
     */
    public static Double toDoubleSafe(Object obj) {
        if (obj == null) {
            return null;
        }

        if (obj instanceof Double) {
            return (Double) obj;
        } else if (obj instanceof Number) {
            return ((Number) obj).doubleValue();
        } else if (obj instanceof String) {
            String str = (String) obj;
            if (!str.isEmpty()) {
                try {
                    return Double.parseDouble(str);
                } catch (NumberFormatException ignored) {
                }
            }
        }

        return null;
    }

    /**
     * 获取数据表的大小（用于调试）
     */
    public static int getCapacityMapSize() {
        return CAPACITY_MAP.size();
    }

    /**
     * 测试示例
     */
    public static void main(String[] args) {
        // 测试精确查询
//        testPoint(77.00, 39.30);     // 整米点
//        testPoint(77.05, 40.06);    // 分米点
        testPoint(80.44, 120.50);    // 厘米点

        // 测试插值
//        testPoint(77.15);           // 需要插值的点
//        testPoint(80.45);           // 用户提供的测试点

        // 边界测试
//        testPoint(77.00);
//        testPoint(92.00);

//        System.out.println("数据表大小: " + getCapacityMapSize() + " 个数据点");
    }
    //

    private static void testPoint(double waterLevel) {
        Double precise = preciseQuery(waterLevel);
        double linear = linearInterpolation(waterLevel);

        System.out.printf("水位 %.2f米：", waterLevel);
        if (precise != null) {
            System.out.printf("精确查询 = %.2f 万立方米 | ", precise);
        } else {
            System.out.printf("精确查询 = 无数据 | ");
        }
        System.out.printf("线性插值 = %.2f 万立方米%n", linear);
    }

    private static void testPoint(double waterLevel, double expected) {
        Double precise = preciseQuery(waterLevel);
        double linear = linearInterpolation(waterLevel);

        System.out.println("========================================");
        System.out.printf("水位 %.2f米 (期望值 %.2f)%n", waterLevel, expected);
        if (precise != null) {
            System.out.printf("精确查询: %.2f  匹配: %s%n", precise,
                    Math.abs(precise - expected) < 0.01 ? "✓" : "✗");
        }
        System.out.printf("线性插值: %.2f  误差: %.2f%%%n",
                linear, 100 * Math.abs(linear - expected) / expected);
        System.out.println("========================================");
    }
}
